﻿using System;
using System.Data;
using MODEL;
using System.Web.UI.WebControls;

public partial class Reg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //txtPassword.Text = "111111";
            //if (txtPassword.TextMode == TextBoxMode.Password)
            //{
            //    txtPassword.Attributes.Add("value", txtPassword.Text);
            //}
            //txtSecondaryPassword.Text = "222222";
            //if (txtSecondaryPassword.TextMode == TextBoxMode.Password)
            //{
            //    txtSecondaryPassword.Attributes.Add("value", txtSecondaryPassword.Text);
            //}
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

        #region 基础判断

        if (CommenMethod.IsMobile(txtUserId.Text.Trim()))
        {
            CommenMethod.GetJavaScript("会员账号不能为手机号，请重新填写！");
            return;
        }
        if (string.IsNullOrEmpty(txtUserId.Text.Trim()))
        {
            CommenMethod.GetJavaScript("会员账号不能为空！");
            return;
        }
        if (txtUserId.Text.Length < 5)
        {
            CommenMethod.GetJavaScript("会员账号最少为5个字符！");
            return;
        }
        if (CommenMethod.CheckIncludeChinese(txtUserId.Text.Trim()))
        {
            CommenMethod.GetJavaScript("会员账号格式不正确,不允许包含中文！");
            return;
        }
        if (txtUserId.Text.Trim().Substring(0, 1) == "0")
        {
            CommenMethod.GetJavaScript("会员账号格式不正确,会员账号不能以0开头！");
            return;
        }
        if (string.IsNullOrEmpty(txtName.Text.Trim()))
        {
            CommenMethod.GetJavaScript("必须输入真实姓名!");
            return;
        }
        if (!CommenMethod.IsMobile(txtTel.Text.Trim()))
        {
            CommenMethod.GetJavaScript("注册会员手机号码格式不正确");
            return;
        }
        //判断手机号是否存在
        bool tbuser = BLL.User.IsExistsMobile(txtTel.Text.Trim());
        if (tbuser)
        {
            CommenMethod.GetJavaScript("手机号已注册，请更换手机号!");
            return;
        }
        if (BLL.User.IsExistUserId(txtUserId.Text.Trim()))
        {
            CommenMethod.GetJavaScript("该会员账号已存在,请重新填写!");
            return;
        }
        if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
        {
            CommenMethod.GetJavaScript("必须输入一级密码");
            return;
        }
        if (string.IsNullOrEmpty(txtSecondaryPassword.Text.Trim()))
        {
            CommenMethod.GetJavaScript("必须输入二级密码");
            return;
        }
        if (string.IsNullOrEmpty(txtRefereeMobile.Text.Trim()))
        {
            CommenMethod.GetJavaScript("必须输入推荐人账号");
            return;
        }
        if (!BLL.User.IsExistUserId(txtRefereeMobile.Text.Trim()))
        {
            CommenMethod.GetJavaScript("该推荐人不存在,请重新填写!");
            return;
        }
        //if (string.IsNullOrEmpty(txtContactPerson.Text.Trim()))
        //{
        //    CommenMethod.GetJavaScript("必须输入接点人账号");
        //    return;
        //}
        //if (!BLL.User.IsExistUserId(txtContactPerson.Text.Trim()))
        //{
        //    CommenMethod.GetJavaScript("该接点人不存在,请重新填写!");
        //    return;
        //}
        //if (string.IsNullOrEmpty(TxtIDNumber.Text.Trim()))
        //{
        //    CommenMethod.GetJavaScript("必须输入身份证号码!");
        //    return;
        //}
        //if (TxtIDNumber.Text.Trim().Length != 18)
        //{
        //    CommenMethod.GetJavaScript("身份证号码格式不正确!");
        //    return;
        //}

        if (HiddenField1.Value == "省份")
        {
            CommenMethod.GetJavaScript("请选择省份！");
            return;
        }
        if (HiddenField2.Value == "地级市")
        {
            CommenMethod.GetJavaScript("请选择地级市！");
            return;
        }
        if (string.IsNullOrEmpty(HiddenField3.Value))
        {
            CommenMethod.GetJavaScript("请选择市、县级市！");
            return;
        }
        if (HiddenField3.Value == "市、县级市")
        {
            CommenMethod.GetJavaScript("请选择市、县级市！");
            return;
        }


        DataTable dtUserInfo = BLL.User.GetReferee(txtRefereeMobile.Text.Trim());
        if (dtUserInfo.Rows.Count > 0)
        {
            if (Int32.Parse(dtUserInfo.Rows[0]["FrozenState"].ToString()) == 0)
            {
                CommenMethod.GetJavaScript("请先激活账号再推荐会员!");
                return;
            }
        }


        #endregion

        User user = new User();
        user.UserID = txtUserId.Text.Trim();
        user.Name = txtName.Text.Trim();
        user.UserLevel = 1;
        user.Password = CommenMethod.md5(txtPassword.Text.Trim(), 32);
        user.SecondaryPassword = CommenMethod.md5(txtSecondaryPassword.Text.Trim(), 32);
        user.RefereeMobile = txtRefereeMobile.Text.Trim();
        user.ContactPerson = string.Empty;
        user.Position = Convert.ToChar(0);
        user.BanDanCenter = string.Empty;
        user.IDNumber = TxtIDNumber.Text.Trim();
        user.OpeningBank = string.Empty;
        user.BankAccount = string.Empty;
        user.Mobile = txtTel.Text.Trim();
        user.IsOut = 0;
        user.RegistUser = txtUserId.Text.Trim();
        user.WeiXin = TxtWeiXin.Text.Trim();
        user.ZhiFuBao = TxtZhiFuBao.Text.Trim();
        user.WeiPay = TxtWeiPay.Text.Trim();
        // user.OpenId = Session["openId"].ToString();
        user.province = HiddenField1.Value;
        user.city = HiddenField2.Value;
        user.county = HiddenField3.Value;
        user.Address = txtAddress.Text;
        BLL.User.INS_UserInfo(user); // 插入会员

        BLL.User.ActiveMember(txtUserId.Text.Trim(), txtUserId.Text.Trim());//激活会员
        CommenMethod.GetJavaScript("注册信息提交成功,请尽快充值报销码！", "Login.aspx");
        btnSubmit.Enabled = false;
    }

    protected void txtUserId_TextChanged(object sender, EventArgs e)
    {
        if (BLL.User.IsExistUserId(txtUserId.Text.Trim()))
        {
            errorTip.Text = "该会员账号已存在,请重新输入！";
        }
        else
        {
            errorTip.Text = "恭喜,该账号可用！";
        }
    }
}